package ru.urfc.portal.rest;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import ru.urfc.portal.managers.NetManager;
import ru.urfc.portal.model.radio.Device;
import ru.urfc.portal.model.radio.NetFrequencyAssignment;
import ru.urfc.portal.model.radio.NetLicense;
import ru.urfc.portal.model.radio.NetWithDevicesNumber;
import ru.urfc.portal.model.radio.NetWithLicense;
import ru.urfc.portal.ui.client.Constants;

@Stateless
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON})
@Path(Constants.CLIENTS + "/{clientId}/" + Constants.NETS)
public class NetService extends BaseService {

	@EJB NetManager netManager;
	
	@GET
	public List<NetWithDevicesNumber> getNets(@PathParam("clientId") Long clientId) {
		return netManager.getNets(clientId);
	}
	
	@GET
	@Path("{netId}")
	public NetWithLicense getNet(@PathParam("clientId") Long clientId, @PathParam("netId") Long netId) {
		return netManager.getNetWithLicense(clientId, netId);
	}
	
	@GET
	@Path("{netId}/" + NET_CHANNELS)
	public List<NetFrequencyAssignment> getNetChannels(@PathParam("clientId") Long clientId, @PathParam("netId") Long netId) {
		return netManager.getNetChannels(clientId, netId);
	}

	@GET
	@Path("{netId}/" + NET_DEVICES)
	public List<Device> getNetDevices(@PathParam("clientId") Long clientId, @PathParam("netId") Long netId) {
		return netManager.getNetDevices(clientId, netId);
	}
	
	@GET
	@Path("{netId}/" + NET_LICENSES)
	public List<NetLicense> listLicenses(@PathParam("clientId") Long clientId, @PathParam("netId") Long netId) {
		return netManager.getNetLicenses(clientId, netId);
	}	

	@GET
	@Path("{netId}/" + NET_LICENSES + "/{licenseId}/" + NET_LICENSE_DOWNLOAD) 
	public String downloadLicense(@PathParam("clientId") Long clientId, @PathParam("netId") Long netId,
			@PathParam("licenseId") Long licenseId, @Context HttpServletResponse response) {
		NetLicense netLicense = netManager.getNetLicense(clientId, netId, licenseId);
		redirect(response, NET_LICENSE_DOWNLOAD, "Лицензия №" + netLicense.number + ".txt");
		return "";
	}
	
}
